$(function(){
	/*** 初始化专业树 */
	$('#subject_tree').tree({
	    url : 'subject/subjects',
	    onClick : function (node) {
	    	var params = {};
	    	var _nodeType = node.nodeType;
	    	var _param = _nodeType + 'Id';
	    	params[_param] = node.id;
	    	//刷新教案列表
	    	$('#dg').datagrid('reload', params);
	    	//刷新知识点数
	    	$('#knowledgeDg').treegrid('reload', params);
	    }
	});
	//知识点列表绑定事件
	$('#knowledgeDg').treegrid({
	    onClickRow:function(row){
	    	$('#dg').datagrid('reload', {
	    		knowledgeId : row.id
	    	});	
	    }
	});
	
	/** 新增 */
	$('#add').click(function() {
		/*$('#form').form('clear');
		$('#win').window('open');*/
		var url = 'lessonplan/add.shtml';
		var node = $('#subject_tree').tree('getSelected');
		var row = $('#knowledgeDg').treegrid('getSelected');
		if(null != node && row != null){
			url = url + '?subjectId=' + node.id + "&knowledgeId=" + row.id;
		}else if(null != node) {
			url = url + '?subjectId=' + node.id;
		}else if(row != null){
			url = url + '?knowledgeId=' + row.id;
		}
		showEasyUiWindow('contentFrame', url, 'win', '新增教案', 625, 350);
	});
	
	/** 编辑 */
	$('#edit').click(function() {
		var row = $('#dg').datagrid('getSelected');
		if (null == row) {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		} else {
			var url = 'lessonplan/edit.shtml?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '编辑教案', 625, 350);
		}
	});
	
	/*** 查看详情 */
	$('#detail').click(function(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			var url = 'lessonplan/detail?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '教案详情', 650, 500);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 删除 */
	$('#remove').click(function(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要删除此条数据吗?', function(r) {
				if (r) {
					var id = row.id;				
					$.get('lessonplan/delete.shtml', {id : id}, function(rs) {
						if (rs.success == true) {
							reSearch();
							$.messager.alert('warning', rs.message, 'warning');
						} else {
							$.messager.alert('warning', rs.message, 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
});

/*** 重载页面 */
function reSearch() {
	$('#title').val('');
	doSearch();
}
/*** 搜索 */
function doSearch() {
	$('#dg').datagrid('reload', {
		title : $('#title').val()
	});
}
/**
 * 将long型转成date型
 * @param value
 * @param row
 * @returns
 */
function formatterDate(value, row) {
	return formatDate(value, 'yyyy-MM-dd hh:mm:ss');
}

function dealClickAtta(attId, fileName) {
	if(isVideo(fileName)) {
		return '<a href="javascript:void(0);" onclick="playVideo(\'' + attId + '\')">' + fileName + '</a>';
	} else {
		return '<a href="' + contextPath + '/attachment/down?attId=' + attId + '" target="_blank">' + fileName + '</a>';
	}
	
}

function isVideo(fileName) {
	var videoStr = "mp4,avi,flv,wmv";
	var suffix = getFileSuffix(fileName);
	if(videoStr.toString().indexOf(suffix)<0){
		suffix = "epub";
	} 
	if(videoStr.toString().indexOf(suffix)<0){
		return false;
	}
	return true;
}

function getFileSuffix(fileName){ 
	if(typeof fileName === 'undefined'){
		return '';
	}else{
		var len = fileName.lastIndexOf('.'); 
		return fileName.substr(len+1,fileName.length);
	}
} 

function playVideo(attId) {
	showEasyUiWindow('contentFrame', 'video/listByAttId.shtml?attId=' + attId, 'win', '视频播放', 640, 380);
}

function dgClickRow(rowIndex, rowData) {
	$('#resource_dg').datagrid('reload', {bizId : rowData.id});
}

function dgOnLoadSuccess(data) {
	if (null != data && data.total > 0) {
		initResourceDg(data.rows[0].id);
	} else {
		 initResourceDg(0);
	}
}

function initResourceDg(bizId) {
	$('#resource_dg').datagrid({    
	    title:"教案附件列表",
		border:0,
		nowrap:true,
		striped:true,
		fit: true,//自动大小
		url:'resource/queryBySubjectId.shtml',
		queryParams : {bizId : bizId},
	    columns : [[
			{field : 'att_id', title : 'att_id', hidden:true},    
	        {field : 'file_name', title : '文件名称', width:300,
	        	formatter : function (value, row, index) {
	        		return dealClickAtta(row.att_id, value);
	        	}
	        }
        ]],
		singleSelect:true,
		rownumbers:true,
		//toolbar:'#tb',
		pagination:false,
		//pageSize:20,
		onClickRow:function(rowIndex, rowData){}
	});
}